//
//  YDCustomerSearchController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/24.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDCustomerSearchController.h"

#import "YDCustomerListCell.h"

#import "YDCustomerListModel.h"

@interface YDCustomerSearchController ()

@property (nonatomic, strong) NSMutableArray *customerArray;

@end

@implementation YDCustomerSearchController

- (instancetype)init
{
    if (self = [super init]) {
        _customerArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView
{
    [self.tableView registerNib(@"YDCustomerListCell")];
    self.tableView.backgroundColor = kViewControllerBackgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (void)viewClick
{
    [self.view endEditing:YES];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.customerArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDCustomerListModel *customerModel = [self.customerArray objectAtIndex:indexPath.row];
    YDCustomerListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDCustomerListCell"];
    cell.customerModel = customerModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YDCustomerListModel *customerModel = [self.customerArray objectAtIndex:indexPath.row];
    if (self.SelectModelBlock) {
        self.SelectModelBlock(customerModel);
    }
}


#pragma mark - 搜索结果
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
   
}

- (void)setSearchKey:(NSString *)searchKey
{
    _searchKey = searchKey;
    
    [self loadCustomerWithSearchKey:searchKey];
}

- (void)loadCustomerWithSearchKey:(NSString *)searchKey
{
    NSDictionary *dic = @{@"searchKey" : searchKey};
    NSString *URL = [NSString stringWithFormat:@"%@sid=%@", kCustomerListAddress, getNSUser(kAPP_SID)];
    
    [YDDataService POST:dic url:URL block:^(id result) {
        
        if ([[result valueForKey:@"code"] isEqualToString:@"S_OK"]) {
            NSArray *custArray = [result valueForKey:@"var"];
            [self updateTableViewWithCustomerArray:custArray];
        }
        
    } failBlock:^(id error) {
        
    }];
}

- (void)updateTableViewWithCustomerArray:(NSArray *)custArray
{
    [self.customerArray removeAllObjects];
    
    for (NSDictionary *dic in custArray) {
        YDCustomerListModel *model = [YDCustomerListModel instanceWithDict:dic];
        [self.customerArray addObject:model];
    }
    [self.tableView reloadData];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
